var skill = {
	hasClassifyById: function(classify_id) {
		var classify_id_list = this._getClassifyIdList();
		return classify_id_list.indexOf(classify_id) !== -1;
	},
	//获取分类详情
	//因为分类并不多所以ClassifyCon中并没有做child缓存子集
	_getClassifyIdList: function() {
		var classify_id_list = this.classifys;
		if (!Array.isArray(classify_id_list)) {
			classify_id_list = this.classifys = [];
			this.save();
		}
		return classify_id_list;
	},
	getClassifyDetail: function(classify_id) {
		var current_classify;
		var current_classify_goods;
		var classify_id_list;
		var bus_classify_id_list = this._getClassifyIdList();
		var child_classify_list;
		var parent_classify_list = [];
		if (classify_id && (current_classify = this._getClassify(classify_id, "no_throw_error"))) {
			classify_id_list = current_classify.childClassifys;
			current_classify_goods = current_classify.getGoods();
			//父级
			var _cur = current_classify;
			while (_cur.parent_classify_id) {
				var _par = ClassifyCon.get(_cur.parent_classify_id);
				parent_classify_list.unshift(_par);
				_cur = _par;
			}
			//子集
			child_classify_list = [];
			classify_id_list.slice().forEach(function(classify_id, index) {
				var classify = ClassifyCon.get(classify_id);
				if (bus_classify_id_list.indexOf(classify_id) !== -1) {
					child_classify_list.push(classify);
				} else {
					classify.destroy();
				}
			});
		} else {
			classify_id_list = bus_classify_id_list;
			//子集
			child_classify_list = [];
			classify_id_list.forEach(function(classify_id, index) {
				var classify = ClassifyCon.get(classify_id);
				//查找顶级的，没有parent
				classify.parent_classify_id || child_classify_list.push(classify);
			});
		}
		return {
			current_classify_goods: current_classify_goods,
			current_classify: current_classify,
			child_classify_list: child_classify_list,
			parent_classify_list: parent_classify_list
		};
	},
	//获取旗下所有商品，包括子目录
	getClassifyAllChildGoods: function(classify_id) {
		var self = this;
		var classify = self._getClassify(classify_id);
		var all_child_goods = Array.prototype.concat.apply(classify.getGoods(), classify.childClassifys.map(function(child_classify_id) {
			return self.getClassifyAllChildGoods(child_classify_id)
		}));
		all_child_goods.sort(function(a, b) {
			return a.create_time < b.create_time;
		});
		return all_child_goods;
	},
	//返回整个目录树
	getClassifyTree: function(parent_classify_id, deep) {
		deep = ~~deep;
		var result = {
			classify: null,
			childs: [],
			deep: deep
		};
		var self = this;
		if (!parent_classify_id) {
			this._getClassifyIdList().forEach(function(classify_id) {
				var classify = ClassifyCon.get(classify_id);
				//查找顶级的，没有parent
				classify.parent_classify_id || result.childs.push(self.getClassifyTree(classify._id, deep + 1));
			});
		} else {
			result.classify = this._getClassify(parent_classify_id);
			result.classify.childClassifys.forEach(function(classify_id) {
				result.childs.push(self.getClassifyTree(classify_id, deep + 1));
			});
		}
		return result;
	},
	//代码片段，根据id获取标签
	_getClassify: function(classify_id, is_silence) {
		var classifys = this._getClassifyIdList();
		var index = classifys.indexOf(classify_id);
		if (index === -1) {
			is_silence || throwCusError("分类不属于指定商家");
			return
		}
		var classify = ClassifyCon.get(classify_id);
		if (!classify) {
			is_silence || throwCusError("找不到指定分类")
			return
		}
		return classify;
	},
	//增加分类
	addClassify: function(classify_name, parent_classify_id) {
		var classify = new ClassifyCon({
			classify_name: classify_name,
			bus_id: this._id, //可有可无
			parent_classify_id: parent_classify_id
		});
		var classifys = this.classifys || (this.classifys = []);
		var classify_id = classify._id;
		classifys.push(classify_id);
		//用sort()return localeCompare 来做文件夹排序？
		this.save();
		return classify;
	},
	//移除分类（不会移除分类下的商品）
	removeClassify: function(classify_id) {
		var classify = this._getClassify(classify_id);
		classify.destroy();
		//将标签从商家标签集合中删除
		this.classifys = $Array.remove(this._getClassifyIdList(), classify_id);
		this.save();
	},
	removeClassifyList: function(classify_id_list) {
		if (!Array.isArray(classify_id_list)) {
			return
		}
		var self = this;
		classify_id_list.forEach(function(classify_id) {
			try {
				self.removeClassify(classify_id)
			} catch (e) {
				console.error("ERROR IN:removeClassifyList")
				console.error(e)
				console.error("----")
			}
		});
	},
	//修改分类
	updateClassify: function(classify_id, classify_info) {
		var classify = this._getClassify(classify_id);
		classify.setInfo(classify_info);
		return classify;
	},
	//修改商品顺序
	updateClassifyGoodsIndex: function(classify_id, goods_id, goods_index, options) {
		var classify = this._getClassify(classify_id, "no_throw_error");
		classify.updateGoodsIndex(goods_id, goods_index, options);
		return this;
	},
	moveGoodsToClassify: function(classify_id, goods_id) {
		var classify = this._getClassify(classify_id, "no_throw_error");
		classify_id = classify && classify._id
		var goods = this._getGoods(goods_id);
		if (goods.classify_id !== classify_id) {
			goods._setClassify(classify_id);
		}
		return {
			classify: classify,
			goods: goods
		};
	},
	//将商品移动到指定分类（包括未分类）
	moveGoodsListToClassify: function(classify_id, goods_id_list) {
		if (!Array.isArray(goods_id_list)) {
			return
		}
		var self = this;
		var classify = this._getClassify(classify_id, "no_throw_error");
		classify_id = classify && classify._id
		var effect_goods_list = [];
		goods_id_list.forEach(function(goods_id) {
			try {
				var goods = self._getGoods(goods_id);
				if (goods.classify_id !== classify_id) {
					goods._setClassify(classify_id);
				}
				effect_goods_list.push(goods);
			} catch (e) {
				console.error("ERROR IN:moveGoodsListToClassify")
				console.error(e)
				console.error("----")
			}
		});
		return {
			goods_list: effect_goods_list,
			classify: classify
		}
	},
	//移动分类文件夹到指定分类文件夹下（或者顶级文件夹）
	moveClassify: function(classify_id, parent_classify_id) {
		var classify = this._getClassify(classify_id);
		classify.setParentClassify(parent_classify_id);
	},
	//启用优惠
	// is_child_enable : 强制所有无独立配置的子类目全部起启用优惠
	// is_cover_child_buff : 即便有独立配置的子目录也给改成继承优惠的模式
	enableClassifyBuff: function(classify_id, info, is_child_enable, is_cover_child_buff) {
		var classify = this._getClassify(classify_id);
		classify._enableBuff(info);
		//是否递归将所有子类目、子子类目全部通过继承方式取得优惠能力
		if (is_child_enable) {
			this.getClassifyTree(classify_id).childs.forEach(function(tree_item) {
				var child_classify = tree_item.classify;
				if (is_cover_child_buff || (!child_classify.enable_buff && !child_classify.type /* == ClassifyCon.TYPE.无*/ )) {
					child_classify._enableBuff({} /*空的info会强制将type滞为TYPE.无*/ );
				}
			});
		}
		return classify;
	},
	//禁用优惠，默认没有独立配置类目在buff触发查找父级的时候会因此而失去优惠能力
	disableClassifyBuff: function(classify_id, is_child_keep_enable) {
		var classify = this._getClassify(classify_id);
		//前提，自己有独立的优惠配置，可能有子类目的优惠配置依赖于自己
		//如果要保持那些没有独立配置优惠的类目的优惠能力，需要把优惠配置传给child，因为继承关系，只需传递一层
		if (classify.enable_buff && classify.type /* >0 */ && is_child_keep_enable) {
			classify.childClassifys.forEach(function(child_classify_id) {
				var child_classify = ClassifyCon.get(child_classify_id);
				//寻找依赖于继承关系的类目，强行注入配置
				if (child_classify.enable_buff && !child_classify.type /* == ClassifyCon.TYPE.无*/ ) {
					child_classify._enableBuff(classify); //将配置传递下去
				}
			});
		}
		classify._disableBuff();
		return classify;
	},
	//修改顺序
	updateClassifyIndex: function(parent_classify_id, classify_id, classify_index) {
		//start-get
		if (!parent_classify_id) {
			var self = this;
			var classify_id_list = this._getClassifyIdList();
		} else {
			self = this._getClassify(parent_classify_id);
			classify_id_list = self.childClassifys;
		}

		//re-index
		console.log(classify_id_list)
		var index = classify_id_list.indexOf(classify_id);
		if (index === -1) {
			throwCusError("标签不属于指定商家")
		}
		classify_id_list.splice(index, 1);
		classify_index = ~~classify_index;
		if (classify_index > classify_id_list.length) {
			classify_id_list.push(classify_id);
		} else if (classify_index < 0) {
			classify_id_list.unshift(classify_id);
		} else {
			classify_id_list.splice(classify_index, 0, classify_id)
		}

		//end-save
		if (!parent_classify_id) {
			self.classifys = classify_id_list;
		} else {
			self.childClassifys = classify_id_list;
		}
		self.save();
		return self;
	},
};
module.exports = skill;

var db = require("../db/index");
var DomainCon = require("../domain/_con");
var GoodsCon = require("../goods/_con");
var UserCon = require("../../core/user/_con");
var BusCon = require("../../core/bus/_con");
var AchCon = require("../ach/_con");
var ClassifyCon = require("../bus.classify/_con");
var TagCon = require("../bus.tag/_con");
var LeagueCon = require("../league/_con");
var BuffCon = require("../buff/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusGuideCon = require("../bus.guide/_con");
var CommentCon = require("../comment/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var helper = require("../_include/helper");
var md5 = require("../_include/md5");
var $Object = require("../_include/$.Object");
var $Array = require("../_include/$.Array");
var tld = require('tldjs');
var fs = require("fs");
var Fiber = require('fibers');
var LBS = require("../_include/baiduLBS");
var midEvent = require("../../middleware/index");